﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using System;
using System.Collections;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SalesModule
{
	#region OtherReceivable

    public class ReceivablesAppService : BaseAppService<Receivables, long, ReceivablesSearch, IReceivablesRepository>, IReceivablesAppService
	{
        private ICustomerAppService CustomerAppService { get; set; }

        public override BoolGenericResult<Receivables> AddOrUpdate(Receivables entity)
        {
            Init();
            this.CustomerAppService = this.SpringContext.GetObject(SystemModuleConst.CUSTOMERAPPSERVICE) as ICustomerAppService;

            BoolGenericResult<Receivables> result = new BoolGenericResult<Receivables>();

            if (entity.Customer.IsNotNull() && entity.Customer.Id.IsNotEmpty())
            {
                entity.Customer = this.CustomerAppService.GetById(entity.Customer.Id);
            }

            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Administrator administrator = new Administrator(employee);

            result = administrator.AddCustomerInitialBalance(entity.CreateDate, entity.ReceivablesType, entity.Amount, entity.Currency, entity.CurrencyRate, entity.Customer);

            if (result.IsSuccess)
            {
                base.AddOrUpdate(result.Object);
            }

            result.Messages.Add("保存成功");

            return result;
        }
	}
	#endregion
}